export default {
  data() {
    return {};
  },
  methods: {
    input_blurFun(item) {
      if (item.columnname == "身份证号") {
        let regName = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if (regName.test(item.value)) {
          let time = this.getBirthdayFromIdCard(item.value);
          let a = this.getSex(item.value) == "man" ? 1 : 2;
          this.form.fields.map((item) => {
            if (item.columnname == "出生日期") {
              item.value = time;
            }
            if (item.columnname == "性别") {
              item.value = a;
            }
          });
        }
      }
    },
    select_cha(item) {
      if (item.columnname == "试用期") {
        this.changeGetToFormal();
      } else if (item.columnname == "合同期限") {
        this.changeGetContractEndDate();
      } else if (item.columnname == "员工状态") {
        // this.rules[item.rule][0].required = item.value == "正式" ? true : false;
        let formitem = this.form.fields.find((m) => m.columnname == "试用期");
        let date = this.form.fields.find((m) => m.columnname == "转正日期");
        this.$refs.form.clearValidate([formitem.rule, date.rule]);
        let tempRule = JSON.parse(JSON.stringify(this.rules[formitem.rule][0]));
        let tempRuleDate = JSON.parse(JSON.stringify(this.rules[date.rule][0]));
        this.rules = { ...this.rules, [formitem.rule]: [], [date.rule]: [] };
        tempRule.required = item.value == 2 ? true : false;
        tempRuleDate.required = item.value == 2 ? true : false;
        this.rules = { ...this.rules, [formitem.rule]: [tempRule], [date.rule]: [tempRuleDate] };
        setTimeout(() => {
          this.$refs.form.validate();
        }, 0);
        if (item.value == 1) {
          date.value = "";
        }
        this.changeGetToFormal();
      }
    },
    //picker改变事件
    datePickerChange(item) {
      debugger;
      if (item.columnname == "入职日期") {
        this.changeGetToFormal();
        let firstWorkDate = this.form.fields.find((m) => m.columnname == "首次工作日期");
        let wordage = this.form.fields.find((m) => m.columnname == "工龄");
        if (item.value && firstWorkDate) {
          if (firstWorkDate.value > item.value) {
            firstWorkDate.value = "";
            wordage.value = "";
            this.$message.warning("首次工作日期不得大于此次入职日期");
            return;
          }
        }
      } else if (item.columnname == "合同起始日") {
        this.changeGetContractEndDate();
      } else if (item.columnname == "首次工作日期") {
        let joinDate = this.form.fields.find((m) => m.columnname == "入职日期");
        let wordage = this.form.fields.find((m) => m.columnname == "工龄");
        if (joinDate && item.value) {
          if (item.value > joinDate.value) {
            item.value = "";
            wordage.value = "";
            this.$message.warning("首次工作日期不得大于此次入职日期");
            return;
          }
        }
        let data = this.dayCha(new Date().toLocaleDateString(), item.value);
        wordage.value = `${data.getDffeYMDHMS.dffeYear > 0 ? `${data.getDffeYMDHMS.dffeYear}年` : ""}${data.getDffeYMDHMS.dffeMonth > 0 ? `${data.getDffeYMDHMS.dffeMonth}月` : ""}${data.getDffeYMDHMS.dffeDay > 0 ? `${data.getDffeYMDHMS.dffeDay}天` : ""}`;
      
        let birth = this.form.fields.find((m) => m.columnname == "出生日期");
        if (item.value < birth.value) {
          item.value = "";
          this.$message.warning("首次工作日期不得小于出生日期");
          return;
        }
      } else if (item.columnname == "出生日期") {
        let firstWorkDate = this.form.fields.find((m) => m.columnname == "首次工作日期");
        if (item.value > firstWorkDate.value) {
          item.value = "";
          this.$message.warning("出生日期不得大于首次工作日期");
          return;
        }
      }
    },

    /**
     * 根据身份证号码获取出生日期
     * @param {*} idCard >身份证号码
     *
     */
    getBirthdayFromIdCard(idCard) {
      var birthday = "";
      if (idCard != null && idCard != "") {
        if (idCard.length == 15) {
          birthday = "19" + idCard.substr(6, 6);
        } else if (idCard.length == 18) {
          birthday = idCard.substr(6, 8);
        }
        birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
      }
      return birthday;
    },

    /**
     * 根据身份证号码获取性别
     * @param {*} idCard >身份证号码
     *
     */
    getSex(idCard) {
      var sexStr = "";
      if (parseInt(idCard.slice(-2, -1)) % 2 == 1) {
        sexStr = "man";
      } else {
        sexStr = "woman";
      }
      return sexStr;
    },

    /**
     * 入职日期&员工类型&员工状态&试用期
     * 改变时调用此事件来计算转正日期
     */
    changeGetToFormal() {
      let baseInfo = this.form.fields.filter((m) => {
        return m.columnname == "入职日期" || m.columnname == "员工状态" || m.columnname == "试用期";
      });
      //取出来的值 必须是3个
      if (!baseInfo && baseInfo.length != 3) {
        return;
      }
      //首先验证员工状态是试用期
      var userStatus = baseInfo.find((m) => m.columnname == "员工状态");
      if (userStatus.options.find((m) => m.optionvalue == userStatus.value)?.optiondisplay != "试用") {
        return;
      }
      var date = baseInfo.find((m) => m.columnname == "入职日期").value;
      if (!date) {
        return;
      }
      var trial = baseInfo.find((m) => m.columnname == "试用期");
      if (trial.value != "0") {
        var tempDate = new Date(date);
        // 1:年  2 :月 3:日
        if (trial.fieldItems[0].value == 1) {
          let newDate = new Date(tempDate.setFullYear(tempDate.getFullYear() + Number(trial.value)));
          newDate = new Date(newDate.setDate(newDate.getDate() - 1));
          this.form.fields.find((m) => m.columnname == "转正日期").value = newDate.toLocaleDateString();
        } else if (trial.fieldItems[0].value == 2) {
          let newDate = new Date(tempDate.setMonth(tempDate.getMonth() + Number(trial.value)));
          newDate = new Date(newDate.setDate(newDate.getDate() - 1));
          this.form.fields.find((m) => m.columnname == "转正日期").value = newDate.toLocaleDateString();
        } else if (trial.fieldItems[0].value == 3) {
          let newDate = new Date(tempDate.setDate(tempDate.getDate() + Number(trial.value)));
          this.form.fields.find((m) => m.columnname == "转正日期").value = newDate.toLocaleDateString();
        }
      } else {
        this.form.fields.find((m) => m.columnname == "转正日期").value = "";
      }
    },

    /**
     * 合同期限&合同起始日
     * 改变时调用此事件来计算合同到期日
     */
    changeGetContractEndDate() {
      let baseInfo = this.form.fields.filter((m) => {
        return m.columnname == "合同起始日" || m.columnname == "合同期限";
      });
      //取出来的值 必须是2个
      if (!baseInfo && baseInfo.length != 2) {
        return;
      }
      var date = baseInfo.find((m) => m.columnname == "合同起始日").value;
      if (!date) {
        return;
      }
      var range = baseInfo.find((m) => m.columnname == "合同期限");

      if (range.value != "0") {
        var tempDate = new Date(date);
        // 1:年  2 :月 3:日
        if (range.fieldItems[0].value == 1) {
          let newDate = new Date(tempDate.setFullYear(tempDate.getFullYear() + Number(range.value)));
          newDate = new Date(newDate.setDate(newDate.getDate() - 1));
          this.form.fields.find((m) => m.columnname == "合同到期日").value = newDate.toLocaleDateString();
        } else if (range.fieldItems[0].value == 2) {
          let newDate = new Date(tempDate.setMonth(tempDate.getMonth() + Number(range.value)));
          newDate = new Date(newDate.setDate(newDate.getDate() - 1));
          this.form.fields.find((m) => m.columnname == "合同到期日").value = newDate.toLocaleDateString();
        } else if (range.fieldItems[0].value == 3) {
          let newDate = new Date(tempDate.setDate(tempDate.getDate() + Number(range.value)));
          this.form.fields.find((m) => m.columnname == "合同到期日").value = newDate.toLocaleDateString();
        }
      } else {
        this.form.fields.find((m) => m.columnname == "合同到期日").value = "";
      }
    },

    dayCha(time, twoTime) {
      time = time.replace(new RegExp("-", "gm"), "/");
      if (twoTime) {
        twoTime = twoTime.replace(new RegExp("-", "gm"), "/");
      } else {
        twoTime = new Date();
      }
      // 计算比较日期
      const getMaxMinDate = (time, twoTime, type) => {
        let minTime = new Date(time).getTime() - new Date(twoTime).getTime() > 0 ? twoTime : time;
        let maxTime = new Date(time).getTime() - new Date(twoTime).getTime() > 0 ? time : twoTime;
        let maxDateDay = new Date(new Date(maxTime).getFullYear(), new Date(maxTime).getMonth() + 1, 0).getDate();
        let maxMinDate = new Date(minTime).getDate() > maxDateDay ? maxDateDay : new Date(minTime).getDate();
        let maxMinTong = null;
        if (type == "month") {
          maxMinTong = new Date(maxTime).getFullYear() + "/" + (new Date(minTime).getMonth() + 1) + "/" + maxMinDate + " " + new Date(minTime).toLocaleTimeString("chinese", { hour12: false });
        } else {
          maxMinTong = new Date(maxTime).getFullYear() + "/" + (new Date(maxTime).getMonth() + 1) + "/" + maxMinDate + " " + new Date(minTime).toLocaleTimeString("chinese", { hour12: false });
        }
        return {
          minTime,
          maxTime,
          maxMinTong,
        };
      };
      // 相差年份
      const getYear = (time, twoTime) => {
        let oneYear = new Date(time).getFullYear();
        let twoYear = new Date(twoTime).getFullYear();
        // 计算出与最小日期的日时分相同的最大日期的值
        // 如2016-8-17和2019-6-30,计算出来的maxMinTong为2019-8-17
        const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, "month");
        // 计算相差多少年
        let chaYear = Math.abs(oneYear - twoYear);
        // 如果计算出的maxMinTong(如2019-8-17)大于了最大日期,则表示最后一年没有满,需要减一
        if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
          chaYear--;
        }
        return chaYear;
      };
      const getMonthTong = (time, twoTime) => {
        let oneMonth = new Date(time).getFullYear() * 12 + (new Date(time).getMonth() + 1);
        let twoMonth = new Date(twoTime).getFullYear() * 12 + (new Date(twoTime).getMonth() + 1);
        return Math.abs(oneMonth - twoMonth);
      };
      // 相差月份
      const getMonth = (time, twoTime, value) => {
        // 计算出与最小日期的日时分相同的最大日期的值
        // 如2016-8-17和2019-6-30,计算出来的maxMinTong为2019-6-17
        const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, "day");
        // 计算最小日期到maxMinTong(如2019-6-17)的月份,这样刚好对应月日
        let chaMonth = getMonthTong(minTime, maxMinTong);
        // 如果计算出来的maxMinTong(如2019-6-17)大于最大日期,则表示最后一月没有满,需要减一
        if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
          chaMonth--;
        }
        if (value) {
          return chaMonth - value;
        } else {
          return chaMonth;
        }
      };
      // 相差天数
      const getDay = (time, twoTime, value) => {
        let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
        if (value) {
          return parseInt(chaTime / 86400000) - value;
        } else {
          return parseInt(chaTime / 86400000);
        }
      };
      // 相差年月日时分秒
      const getDffeYMDHMS = (time, twoTime) => {
        // 计算出与最小日期的日时分相同的最大日期的值
        // 如2016-8-17和2019-6-30,计算出来的maxMinTong为2019-6-17
        const { minTime, maxTime, maxMinTong } = getMaxMinDate(time, twoTime, "day");
        // 计算最小日期到(如:2019-6-17)的天数,这样算出来的天数刚好数足月的
        let dffeDay1 = getDay(minTime, maxMinTong);
        // 判断一下,如果计算出来的(如:2019-6-17)大于最后的日期(如2019-6-30)就需要减去(2019-6-30到2019-6-17的天数,否则不需要)
        if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
          let prevMonth = new Date(maxMinTong).getMonth() - 1;
          let lastTime = new Date(maxMinTong).setMonth(prevMonth);
          dffeDay1 = dffeDay1 - getDay(new Date(lastTime).getFullYear() + "/" + (new Date(lastTime).getMonth() + 1) + "/" + new Date(lastTime).getDate(), maxMinTong);
        }
        let dffeYear = getYear(time, twoTime);
        let dffeMonth = getMonth(time, twoTime, dffeYear * 12);
        let dffeDay = getDay(time, twoTime, dffeDay1);
        console.log(`年:${dffeYear},月:${dffeMonth},日:${dffeDay}`);
        return { dffeYear, dffeMonth, dffeDay };
      };
      return {
        getDffeYMDHMS: getDffeYMDHMS(time, twoTime),
      };
    },
  },
};
